package com.woniuxy.factory.controller;

import com.woniuxy.factory.dto.EquipmentDTO;
import com.woniuxy.factory.entity.Equipment;
import com.woniuxy.factory.entity.EquipmentType;
import com.woniuxy.factory.entity.ResponseResult;
import com.woniuxy.factory.service.EquipmentService;
import com.woniuxy.factory.service.EquipmentTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/equipment")
public class EquipmentController {
    @Autowired
    EquipmentService equipmentService;

    @ResponseBody
    @RequestMapping("/show")
    public ResponseResult showEquipmentType(Equipment equipment, Integer page, Integer limit) {
        ResponseResult<Object> res = new ResponseResult<>();
        //非空判断
        //Integer tid = equipment.getTid();
        if (page == null || limit == null) {
            res.setCode(1);
            res.setMsg("参数不合法");
            return res;
        }
        //业务逻辑
        //根据条件查询设备
        try {
            List<EquipmentDTO> equipments = equipmentService.findEquipmentByCondtion(equipment, page, limit);
            //根据条件查询设备总条数
            Integer count = equipmentService.totalEquipmentByCondtion(equipment);
            //装配
            res.setCode(0);
            res.setMsg("查询成功");
            res.setCount(count);
            res.setData(equipments);
            return res;
        } catch (Exception e) {
            res.setCode(1);
            res.setMsg(e.getMessage());
            return res;
        }

    }

    @ResponseBody
    @RequestMapping("/modify")
    public ResponseResult modifyEquipmentType(Equipment equipment) {
        ResponseResult<Object> res = new ResponseResult<>();
        Integer tid = equipment.getTid();
        Integer eid = equipment.getEid();
        String wosId = equipment.getWosId();
        if (eid == null || tid == null || wosId == null || wosId.isEmpty()) {
            res.setCode(1);
            res.setMsg("参数不合法");
            return res;
        }
        equipmentService.modifyEquipment(equipment);
        res.setCode(0);
        res.setMsg("修改成功");
        return res;
    }

    @ResponseBody
    @RequestMapping("/delet")
    public ResponseResult deletEquipmentById(Integer eid) {
        ResponseResult<Object> res = new ResponseResult<>();
        if (eid == null) {
            res.setCode(1);
            res.setMsg("参数不合法");
            return res;
        }
        equipmentService.deletEquipmentByEid(eid);
        res.setCode(0);
        res.setMsg("删除成功");
        return res;
    }

    @ResponseBody
    @RequestMapping("/add")
    public ResponseResult addEquipment(Equipment equipment) {
        ResponseResult<Object> res = new ResponseResult<>();
        Integer tid = equipment.getTid();
        String model = equipment.getModel();
        String brand = equipment.getBrand();
        String standards = equipment.getStandards();
        String wosId = equipment.getWosId();
        Double price = equipment.getPrice();
        String state = equipment.getState();
        //非空判断
        if (tid == null || model == null || brand == null || standards == null || wosId == null || price == null || state == null) {
            res.setCode(1);
            res.setMsg("参数不合法");
            return res;
        }
        if (model.isEmpty() || brand.isEmpty() || standards.isEmpty() || wosId.isEmpty() || state.isEmpty()) {
            res.setCode(1);
            res.setMsg("参数不合法");
            return res;
        }
        equipmentService.addEquipemt(equipment);
        res.setCode(0);
        res.setMsg("添加成功");
        return res;
    }
}
